package com.anolesoft.yr.mbus.part.mapper;

import com.anolesoft.yr.mbus.part.entity.PartTypeDO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.LocalDateTimeTypeHandler;

import java.util.List;

/**
 * @author zhj
 * @date 2020/8/11 10:37
 */
@Mapper
public interface PartTypeMapper {
    @Insert("insert into part_type (" +
            "objuid," +
            "fac_code," +
            "type_code," +
            "type_name," +
            "c_user_uid," +
            "c_time," +
            "m_user_uid," +
            "m_time," +
            "del_flag," +
            "remark" +
            ")" +
            "values (" +
            "#{objuid,jdbcType=VARCHAR}," +
            "#{factoryCode,jdbcType=VARCHAR}," +
            "#{typeCode,jdbcType=VARCHAR}," +
            "#{typeName,jdbcType=VARCHAR}," +
            "#{createUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "#{modifyUserUid,jdbcType=VARCHAR}," +
            "now()," +
            "0," +
            "#{remark,jdbcType=VARCHAR}" +
            ")")
    int insert(PartTypeDO partTypeDO);

    @Update("update part_type set " +
            "fac_code = #{factoryCode,jdbcType=VARCHAR}," +
            "type_code = #{typeCode,jdbcType=VARCHAR}," +
            "type_name = #{typeName,jdbcType=VARCHAR}," +
            "m_user_uid = #{modifyUserUid,jdbcType=VARCHAR}," +
            "m_time = now()," +
            "remark = #{remark,jdbcType=VARCHAR} " +
            "where objuid = #{objuid,jdbcType=VARCHAR}")
    int update(PartTypeDO partTypeDO);

    @Update("update part_type set del_flag = 1 where objuid = #{id,jdbcType=VARCHAR}")
    int delete(@Param("id") String id);

    @Results(id = "PartTypeResultMap", value = {
            @Result(id = true, column = "objuid", property = "objuid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fac_code", property = "factoryCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type_code", property = "typeCode", jdbcType = JdbcType.VARCHAR),
            @Result(column = "type_name", property = "typeName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_user_uid", property = "createUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "c_time", property = "createTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "m_user_uid", property = "modifyUserUid", jdbcType = JdbcType.VARCHAR),
            @Result(column = "m_time", property = "modifyTime", typeHandler = LocalDateTimeTypeHandler.class),
            @Result(column = "del_flag", property = "deleteFlag", jdbcType = JdbcType.BOOLEAN),
            @Result(column = "remark", property = "remark", jdbcType = JdbcType.VARCHAR),

    })
    @Select("select objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag = 0 ")
    List<PartTypeDO> selectAll();

    @ResultMap("PartTypeResultMap")
    @Select("select objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag = 0 ")
    List<PartTypeDO> selectPageAll(Page<PartTypeDO> page);

    @ResultMap("PartTypeResultMap")
    @Select("select objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag = 0 and objuid = #{id,jdbcType=VARCHAR}")
    PartTypeDO selectOneById(@Param("id") String id);

    @ResultMap("PartTypeResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.typeCode !=null and o.typeCode != '' \">" +
            "   and type_code like #{o.typeCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.typeName !=null and o.typeName != '' \">" +
            "   and type_name like #{o.typeName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartTypeDO> selectList(@Param("o") PartTypeDO PartTypeDO);

    @ResultMap("PartTypeResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag =0 " +
            "<if test =\"o.factoryCode !=null and o.factoryCode != '' \">" +
            "   and fac_code = #{o.factoryCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.typeCode !=null and o.typeCode != '' \">" +
            "   and type_code like #{o.typeCode, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.typeName !=null and o.typeName != '' \">" +
            "   and type_name like #{o.typeName, jdbcType=VARCHAR} " +
            "</if>" +
            "<if test =\"o.objuid !=null and o.objuid != '' \">" +
            "   and objuid = #{o.objuid, jdbcType=VARCHAR} " +
            "</if>" +
            "</script>")
    List<PartTypeDO> selectPageList(@Param("o") PartTypeDO PartTypeDO, Page<PartTypeDO> page);

    @ResultMap("PartTypeResultMap")
    @Select("<script>" +
            "select " +
            "objuid, fac_code, type_code, type_name, " +
            "c_user_uid, c_time, m_user_uid, m_time, del_flag, remark " +
            "from part_type where del_flag =0 " +
            "<if test=\"o.typeCode!=null or o.typeName!=null \">" +
                "and (" +
                    "<trim prefixOverrides=\"or\">" +
                        "<if test =\"o.typeCode !=null and o.typeCode != '' \">" +
                        "  or type_code like concat(#{o.typeCode, jdbcType=VARCHAR},'%') " +
                        "</if>" +
                        "<if test =\"o.typeName !=null and o.typeName != '' \">" +
                        "  or type_name like concat(#{o.typeName, jdbcType=VARCHAR},'%') " +
                        "</if>" +
                    "</trim>" +
                ")" +
            "</if>" +
            "</script>")
    List<PartTypeDO> selectComboxList(@Param("o") PartTypeDO partTypeDO, Page<PartTypeDO> page);

    @Select("select count(objuid) from part_type where del_flag = 0 and type_code = #{typeCode, jdbcType=VARCHAR}")
    Integer checkUniqueForTypeCode(@Param("typeCode") String typeCode);

    @Select("select count(objuid) from part_type where del_flag = 0 and type_name = #{typeName, jdbcType=VARCHAR}")
    Integer checkUniqueForTypeName(@Param("typeName") String typeName);

    @Select("select count(objuid) from part_type where del_flag = 0 and type_code = #{typeCode, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForTypeCodeUpdate(@Param("typeCode") String typeCode, @Param("id") String id);

    @Select("select count(objuid) from part_type where del_flag = 0 and type_name = #{typeName, jdbcType=VARCHAR} and objuid != #{id, jdbcType=VARCHAR}")
    Integer checkUniqueForTypeNameUpdate(@Param("typeName") String typeName, @Param("id") String id);
}
